//
//  TestCoreTextView_03.m
//  BookTest1
//
//  Created by IOS3 on 2018/11/6.
//  Copyright © 2018 IOS3. All rights reserved.
//

#import "TestCoreTextView_03.h"
#import <CoreText/CoreText.h>

@implementation TestCoreTextView_03




static CGFloat ascentCallback(void *ref){
    return [(NSNumber*)[(__bridge NSDictionary*)ref objectForKey:@"height"] floatValue];
}
static CGFloat descentCallback(void *ref){
    return 0;
}
static CGFloat widthCallback(void* ref){
    return [(NSNumber*)[(__bridge NSDictionary*)ref objectForKey:@"width"] floatValue];
}



- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    NSString *str = @"这是一段文字"
                    "用于 学习 CoreText"
                    "what the fuck, english words?"
                    "最后一行";
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:str];
    
    // 对 attrString 进行样式设置
    
    [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, [attrString length])];
    
    //
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, rect);
    
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, [attrString length]), path, NULL);
    
    
    UIImage *image = [UIImage imageWithContentsOfFile: [NSBundle.mainBundle pathForResource:@"test001" ofType:@"png"]];
    
    NSDictionary *dict = @{@"height" : @708, @"width" : @314};

    CTRunDelegateCallbacks callbacks;
    memset(&callbacks, 0, sizeof(CTRunDelegateCallbacks));
    callbacks.version = kCTRunDelegateVersion1;
    callbacks.getAscent = ascentCallback;
    callbacks.getDescent = descentCallback;
    callbacks.getWidth = widthCallback;
    CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (__bridge void*)dict);
    
    // 使用 0xFFFC 作为空白的占位符
    unichar objectReplacementChar = 0xFFFC;
    NSString * content = [NSString stringWithCharacters:&objectReplacementChar length:1];
//    NSDictionary * attributes = [self attributesWithConfig:config];
//    NSMutableAttributedString * space =
//    [[NSMutableAttributedString alloc] initWithString:content
//                                           attributes:attributes];
//    CFAttributedStringSetAttribute((CFMutableAttributedStringRef)space,
//                                   CFRangeMake(0, 1), kCTRunDelegateAttributeName, delegate);
    CFRelease(delegate);
    
    
    
    
    
    // 坐标翻转
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    //压栈，压入图形状态栈中.每个图形上下文维护一个图形状态栈，并不是所有的当前绘画环境的图形状态的元素都被保存。图形状态中不考虑当前路径，所以不保存
    //保存现在得上下文图形状态。不管后续对context上绘制什么都不会影响真正得屏幕。
    CGContextSaveGState(context);
    //x，y轴方向移动
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    //缩放x，y轴方向缩放，－1.0为反向1.0倍,坐标系转换,沿x轴翻转180度
    CGContextScaleCTM(context, 1.0, -1.0);
    
    // CoreText的坐标原点在左下, UIKit的坐标原点在左上
    // 以上做法是 context 向下移动其高度, 再
    
    
    // 文字绘制
    CTFrameDraw(frame, context);
    
    CFRelease(frame);
    CFRelease(path);
    CFRelease(frameSetter);
}

@end
